package com.xuzong.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuzong.mapper.TbAreaMapper;
import com.xuzong.pojo.TbArea;
import com.xuzong.pojo.TbWarehouse;
import com.xuzong.service.TbAreaService;
import com.xuzong.service.TbWarehouseService;
import com.xuzong.mapper.TbWarehouseMapper;
import com.xuzong.util.PageInfoVo;
import com.xuzong.util.R;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author lenovo
* @description 针对表【tb_warehouse】的数据库操作Service实现
* @createDate 2024-08-02 22:06:26
*/
@Service
public class TbWarehouseServiceImpl extends ServiceImpl<TbWarehouseMapper, TbWarehouse>
    implements TbWarehouseService{

    @Autowired
    TbWarehouseMapper tbWarehouseMapper;

    @Autowired
    TbAreaMapper tbAreaMapper;

    @Override
    public R warehouseList(PageInfoVo pageInfoVo) {


        Page<TbWarehouse> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());

        QueryWrapper<TbWarehouse> wrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){

            wrapper.lambda().like(TbWarehouse::getName,pageInfoVo.getKeyWord());

        }

        if(StringUtils.isNotEmpty(pageInfoVo.getBeginTime()) && StringUtils.isNotEmpty(pageInfoVo.getEndTime())){

            wrapper.lambda().ge(TbWarehouse::getCreateTime,pageInfoVo.getBeginTime())
                    .le(TbWarehouse::getCreateTime,pageInfoVo.getEndTime());

        }

        Page<TbWarehouse> pageList = tbWarehouseMapper.selectPage(page, wrapper);

        List<TbWarehouse> list = pageList.getRecords();

        for (TbWarehouse tbWarehouse : list) {
            TbArea tbArea = tbAreaMapper.selectById(tbWarehouse.getProvinceId());
            tbWarehouse.setProName(tbArea.getAreaName());
            TbArea tbArea1 = tbAreaMapper.selectById(tbWarehouse.getCityId());
            tbWarehouse.setCityName(tbArea1.getAreaName());
            TbArea tbArea2 = tbAreaMapper.selectById(tbWarehouse.getCountyId());
            tbWarehouse.setCountName(tbArea2.getAreaName());
        }

        return R.ok(list);
    }

    /**
     * @description: 添加
     * @author: xushuaihang
     * @Date: 2024/8/4 18:16
     * @param:
     * @param tbWarehouse
     * @return: com.xuzong.util.R
    **/
    @Override
    public R addWarehouse(TbWarehouse tbWarehouse) {
        String code = "";
        //qg84MER6abS4
        if(tbWarehouse.getCode() == 1){
           code = "qg"+ RandomStringUtils.
                    randomAlphabetic(4).toLowerCase()+
                    System.currentTimeMillis()+"".substring(0,4);
        }else if(tbWarehouse.getCode() == 2){

           code = "qg"+RandomUtil.BASE_CHAR.toLowerCase().substring(0,8);
        }else if(tbWarehouse.getCode() == 3){
            String s2 = System.currentTimeMillis() + "";
           code = "qg"+ s2.substring(0,8);

        }else if(tbWarehouse.getCode() == 4){
            code = "qg"+RandomStringUtils.randomAlphabetic(8).toUpperCase();
        }
        tbWarehouse.setSecureCode(code);

        tbWarehouseMapper.insert(tbWarehouse);

        return R.ok();
    }
}




